package com.nuoniu.sibanyun.controller.erp.in;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.UsedGradeRuleBo;
import com.nuoniu.sibanyun.entity.erp.in.InInventory;
import com.nuoniu.sibanyun.entity.erp.in.InInventoryLocation;
import com.nuoniu.sibanyun.entity.erp.in.InWarehouse;
import com.nuoniu.sibanyun.entity.inOut.VInOutRecordDetail;
import com.nuoniu.sibanyun.entity.inOut.VInOutRecordMain;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpAccountingService;
import com.nuoniu.sibanyun.service.erp.IErpGradeRuleService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryLocationService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryService;
import com.nuoniu.sibanyun.service.erp.in.IInWarehouseService;
import com.nuoniu.sibanyun.service.inOut.IVInOutRecordDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 仓库管理 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/in/warehouse")
@Api(value = "仓库管理", tags = "仓库管理")
public class InWarehouseController extends BaseController {

    @Autowired
    private IInWarehouseService inWarehouseService;
    @Autowired
    private IInInventoryLocationService locationService;
    @Autowired
    private IInInventoryService inventoryService;
    @Autowired
    private IErpAccountingService accountingService;
    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IVInOutRecordDetailService inOutRecordDetailService;

    @Autowired
    private IErpGradeRuleService erpGradeRuleService;

    /**
     * 获取仓库列表
     * @author 小啊刚
     * @date 2020-09-23
     */
    @GetMapping("pageList")
    @ApiOperation(value = "获取仓库列表")
    public HttpResult<InWarehouse> pageList(Page page, InWarehouse inWarehouse) {
        inWarehouse.setAccountId(accountService.getCurrentAccountId());
        return HttpResult.ok(inWarehouseService.pageList(page, inWarehouse));
    }

    @GetMapping("listMyOperationInWarehouses")
    @ApiOperation(value = "获取(库管员自己可以操作的)仓库列表")
    public HttpResult listMyOperationInWarehouses(String keyWord) {
        List<InWarehouse> inWarehouses = inWarehouseService.listMyOperationInWarehouses(keyWord);
        Page<InWarehouse> page = new Page<>();
        page.setRecords(inWarehouses);
        page.setSize(inWarehouses.size());
        page.setTotal(inWarehouses.size());
        return HttpResult.ok(page);
    }

    /**
     * 保存仓库
     * @author 小啊刚
     * @date 2020-09-23
     */
    @PostMapping("save")
    @ApiOperation(value = "保存仓库")
    public HttpResult save(@RequestBody InWarehouse inWarehouse) {
       inWarehouseService.saveInWarehouse(inWarehouse);
        return HttpResult.ok();
    }

    /**
     * 删除仓库
     * @author 小啊刚
     * @date 2020-09-23
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除仓库")
    public HttpResult delete(@RequestBody List<Integer> ids) {
        if(ids.size() < 1) {
            throw new NuoNiuException("请选择要删除的仓库");
        }

        List<InInventoryLocation> location = locationService.getByWarehouse(StringUtils.join(ids, ","));
        if(location.size() > 0) {
            throw new NuoNiuException("删除失败,该仓库下有货位");
        }

        List<VInOutRecordMain> inOutRecordDetails =  inOutRecordDetailService.getByWarehouse(StringUtils.join(ids, ","));
        if(inOutRecordDetails.size() > 0) {
            throw new NuoNiuException("该仓库已在出入库单据中使用,不能删除");
        }

        List<InInventory> inventories = inventoryService.getByWarehouse(StringUtils.join(ids, ","));
        if(inventories.size() > 0) {
            throw new NuoNiuException("该仓库已在物料档案中使用,不能删除");
        }
        inWarehouseService.removeByIds(ids);


        // 清除缓存
        List<String> keepers = inWarehouseService.getKeepers(ids);
        if(null != keepers){
            Set<String> clearUserIds = new HashSet<>();
            keepers.forEach(keeper->{
                if(StringUtils.isNotBlank(keeper)){
                    String[] whKeeperArr = keeper.split(",");
                    List<String> whKeeperList = Arrays.asList(whKeeperArr);
                    clearUserIds.addAll(whKeeperList);
                }
            });
            if(clearUserIds.size() > 0){
                inWarehouseService.clearMyWarehouseIdsRedis(clearUserIds);
            }
        }
        return HttpResult.ok();
    }
}
